package com.sens.network;

import android.content.Context;

import com.sens.bean.BaseJson;
import com.sens.database.LiteOrmDBUtil;
import com.sens.tables.AlipayOrder;
import com.yanzhenjie.nohttp.rest.Response;

public class AlipayLogCallback extends ResponseCallback<BaseJson> {
    private Context context;
    private AlipayOrder alipayOrder;
    private BaseJson baseJson;
    private int retryCount;

    public AlipayLogCallback(Context context, AlipayOrder alipayOrder, BaseJson baseJson) {
        this.context = context;
        this.alipayOrder = alipayOrder;
        this.baseJson = baseJson;
    }

    @Override
    public void onSucceed(int what, Response<BaseJson> response) {
        super.onSucceed(what, response);
        BaseJson result = response.get();
        if (result != null)
            if (result.getStatus() == 1) {
                alipayOrder.setHasUpload(true);
                alipayOrder.setHasUploadLog(true);
                LiteOrmDBUtil.getGlobalDBUtil(context).update(alipayOrder);
            } else {
                onFailed(what, response);
            }
    }

    @Override
    public void onFailed(int what, Response<BaseJson> response) {
        super.onFailed(what, response);
        if (retryCount > 1) {
            context = null;
            alipayOrder = null;
            baseJson = null;
            return;
        }
        retryCount++;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    ApiByHttp.getInstance().uploadAlipayLog(alipayOrder, baseJson, AlipayLogCallback.this);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}